package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.model.Policy;
import com.qy.model.PolicyOrder;
import com.qy.service.PolicyOrderService;
import com.qy.service.PolicyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-PolicyOrderController")
@RequestMapping("/manage/policy/order")
public class PolicyOrderController {
    @Resource
    private PolicyOrderService policyOrderService;
    @Resource
    private PolicyService policyService;

    @PostMapping("/add")
    public Result add(@RequestBody PolicyOrder policyOrder) {
        policyOrderService.save(policyOrder);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        policyOrderService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody PolicyOrder policyOrder) {
        policyOrderService.update(policyOrder);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        PolicyOrder policyOrder = policyOrderService.findById(id);
        return ResultGenerator.successResult(policyOrder);
    }

    @GetMapping("/list")
    public Result list(PageBean<PolicyOrder> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<PolicyOrder> list = policyOrderService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //    订单管理-政策订单-列表
    @GetMapping("/selectPolicyOrderList")
    public Result selectPolicyOrderList(PageBean<Map> page, Integer type, String content, PolicyOrder policyOrder) {
        if (type == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        } else if (type == 1) {
            policyOrder.setOrder_number(content);
        } else if (type == 2) {
            policyOrder.setName(content);
        } else if (type == 3) {
            policyOrder.setPhone(content);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize() ,"add_time desc");
        List<Map> list = policyOrderService.selectPolicyOrderList(policyOrder);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //    订单管理-政策订单-查看
    @GetMapping("/seePolicyOrder")
    public Result seePolicyOrder(Integer id) {
        if (id == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Map> list = policyOrderService.seePolicyOrder(id);
        return ResultGenerator.successResult(list);
    }

//    订单-获取手机号
    @GetMapping("/getPhone")
    public Result getPhone(Integer id){
        if (id == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        policyOrderService.getPhone(id);
        return ResultGenerator.successResult();
    }
}
